﻿using NetMQ;
using NetMQ.Sockets;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace iS3.Server.Core.NetMQ
{
    public  class NetMQManager
    {
        public string NetMQServerUrl { get; set; }
        public string NetMQClientId { get; set; }
        public NetMQSocket Client { get; set; }
        private static NetMQManager netMQManager;
        public static NetMQManager getInstance()
        {
            if (netMQManager == null)
            {
                netMQManager = new NetMQManager();
            }
            return netMQManager;
        }
        NetMQManager()
        {

            NetMQServerUrl = ConfigurationManager.AppSettings["NetMQServerUrl"];
            
        }
        public void initServer()
        {

            Client = new ResponseSocket();
            try
            {
                Client.Bind(NetMQServerUrl);
          
                Console.WriteLine("成功创建");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            //Client.SendFrame(num + ":" + message);
        }
        public void SendMessage(string message)
        {
            Console.WriteLine(NetMQClientId + "@" + message);
            Client.SendFrame(NetMQClientId + "@" + message);
            string answer = Client.ReceiveFrameString();
        }
        public void initClient()
        {
            if (NetMQServerUrl != null)
            {
                NetMQClientId = ConfigurationManager.AppSettings["ServerID"] + Guid.NewGuid().ToString();
                Client = new RequestSocket();
                try
                {
                    Client.Connect(NetMQServerUrl);
                    Console.WriteLine("连接成功");
                    //Client.SendFrame("universal server");
                    //string answer = Client.ReceiveFrameString();

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }

                //Client.SendFrame(num + ":" + message);
            }

        }
    }
}
